package com.hudage.kotlinmvp.net

import android.net.ParseException
import android.util.Log
import com.google.gson.JsonParseException
import org.json.JSONException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException

class ExceptionHandle {
    companion object {
        var errorCode = ErrorStatus.UNKNOWN_ERROR
        var errorMsg = "请求失败，请稍后重试"

        fun handleException(e: Throwable): String {
            e.printStackTrace()
            when (e) {
                is SocketTimeoutException -> {//网络超时
                    Log.e("TAG", "网络连接异常: " + e.message)
                    errorMsg = "网络连接异常"
                    errorCode = ErrorStatus.NETWORK_ERROR
                }
                is ConnectException -> { //均视为网络错误
                    Log.e("TAG", "网络连接异常: " + e.message)
                    errorMsg = "网络连接异常"
                    errorCode = ErrorStatus.NETWORK_ERROR
                }
                is JsonParseException, is JSONException, is ParseException -> {   //均视为解析错误
                    Log.e("TAG", "数据解析异常: " + e.message)
                    errorMsg = "数据解析异常"
                    errorCode = ErrorStatus.SERVER_ERROR
                }
                is ApiException -> {//服务器返回的错误信息
                    errorMsg = e.message.toString()
                    errorCode = ErrorStatus.SERVER_ERROR
                }
                is UnknownHostException -> {
                    Log.e("TAG", "网络连接异常: " + e.message)
                    errorMsg = "网络连接异常"
                    errorCode = ErrorStatus.NETWORK_ERROR
                }
                is IllegalArgumentException -> {
                    errorMsg = "参数错误"
                    errorCode = ErrorStatus.SERVER_ERROR
                }
                else -> {//未知错误
                    try {
                        Log.e("TAG", "错误: " + e.message)
                    } catch (e1: Exception) {
                        Log.e("TAG", "未知错误Debug调试 ")
                    }

                    errorMsg = "未知错误，可能抛锚了吧~"
                    errorCode = ErrorStatus.UNKNOWN_ERROR
                }
            }
            return errorMsg
        }

    }
}